/**
 * @author: Loy 
 * @date: 2014-7-2-下午3:44:08
 */
package net.loyintean.pearlshell.sm.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import net.loyintean.pearlshell.base.model.ILogInfo;
import net.loyintean.pearlshell.base.model.IResult;
import net.loyintean.pearlshell.base.model.impl.LogInfoFactory;

/**
 * defined some basic field and method for struts2.0 action
 * 
 * @author Loy
 * @version 2014-7-2.1.0.0
 */
public class BaseAction extends ActionSupport {

	/**
	 *
	 */
	private static final long serialVersionUID = 5890939984997564333L;

	/**
	 * 日志记录器
	 * 
	 * @author 團長
	 * @since 2013-5-15
	 */
	protected Logger logger = Logger.getLogger(this.getClass());

	/**
	 * flag for success or fail.
	 * 
	 */
	private boolean success = true;

	/**
	 * message about execute result.
	 * 
	 */
	private String message = SUCCESS;

	/**
	 * defined the iloginfo for the action.
	 * <p>
	 * the default value is {@link #toString()}
	 * 
	 * @return
	 */
	protected ILogInfo recordLoggerPrefix() {
		return LogInfoFactory.BUILD(this.toString());
	}

	/**
	 * 处理校验错误
	 * <p>
	 * 设置“操作成功标志位”为false； 设置“操作信息”为入参message； 记录日志；返回{@link #INPUT}
	 * 
	 * 
	 * @author Loy
	 * @since 2014-4-10
	 * @param _message
	 *            校验错误提示信息
	 * @param logInfo
	 *            记录日志所使用的桩信息
	 * @return 始终返回{@link #INPUT}
	 */
	protected String validFailed(String _message, ILogInfo logInfo) {
		this.success = false;
		this.message = _message;
		logger.error(logInfo.error("validFaild-&message=" + message));
		return INPUT;

	}

	/**
	 * 处理执行失败。没有异常，但是执行失败。
	 * <p>
	 * 设置“操作成功标志位”为false； 设置“操作信息”为入参message； 记录日志；返回{@link #SUCCESS}
	 * <p>
	 * 2014-3-14 Loy as the fail is a business branch, this method should return
	 * SUCCESS.
	 * 
	 * @author Loy
	 * @since 2014-4-10
	 * @param _message
	 *            异常返回的提示信息
	 * @param logInfo
	 *            记录日志所使用的桩信息
	 * @return 始终返回{@link #SUCCESS}
	 */
	protected String executeFailed(String _message, ILogInfo logInfo) {
		this.success = false;
		this.message = _message;
		logger.error(logInfo.error("executeExcepted-&message=" + message));
		return SUCCESS;

	}

	/**
	 * 处理执行异常
	 * <p>
	 * 设置“操作成功标志位”为false； 设置“操作信息”为入参message； 记录日志；返回{@link #ERROR}
	 * 
	 * @author 團長
	 * @since 2013-11-26
	 * @param e
	 *            发生的异常
	 * @param _message
	 *            异常返回的提示信息
	 * @param logInfo
	 *            记录日志所使用的桩信息
	 * @return 始终返回{@link #ERROR}
	 */
	protected String executeExcepted(Exception e, String _message,
			ILogInfo logInfo) {
		this.success = false;
		this.message = _message;
		logger.error(logInfo.error("executeExcepted-&message=" + message
				+ "&exception=" + e));
		return ERROR;

	}

	/**
	 * deal with execute result and return a string indecate the action result.
	 * 
	 * @param executeResult
	 *            execute result of the action
	 * @param logInfo
	 *            loginfo of the method.
	 * @return SUCCESS if executeResult.isSuccess() and isFail(), ERROR if
	 *         isException() and
	 */
	protected String executeResult(
			@SuppressWarnings("rawtypes") IResult executeResult,
			ILogInfo logInfo) {
		String result = SUCCESS;

		if (executeResult.isException()) {
			// 异常情况
			result = executeExcepted(executeResult.getException(),
					executeResult.getMessage(), logInfo);

		} else if (!executeResult.isSuccess()) {
			// 失败、初始化状态
			result = executeFailed(executeResult.getMessage(), logInfo);
		}
		return result;
	}

	/**
	 * @return the {@link #success }
	 */
	public boolean isSuccess() {
		return success;
	}

	/**
	 * @param success
	 *            the {@link #success }to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}

	/**
	 * @return the {@link #msg }
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param msg
	 *            the {@link #msg }to set
	 */
	public void setMessage(String msg) {
		this.message = msg;
	}

}
